package util;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.sam.widget.R;
import com.sam.widget.headerBar.BaseHeaderView;

/**
 * 中间为SegmnetedBar的标题栏，用法：
 * header.setSegmentInfo(new String[]{"消息", "电话"}, 0,new SegmentedBar.OnSegmentCheckedChangeListener() { ......});
 */
public class BaseSegmentedBar extends BaseHeaderView {
    @ColorInt
    private int segmentedCheckedColor = Color.WHITE;
    private int segmentedTextSize;
    private SegmentedBar segmentedBar;
    private float segmentedCornerRadius;
    private int segmentedBorderWidth;
    private int segmentedItemWidth,segmentedItemHeight;
    private int segmentCheckedPostion = -1;

    public BaseSegmentedBar(Context context) {
        super(context);
    }

    public BaseSegmentedBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseSegmentedBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void getAttrs(AttributeSet attrs) {
        super.getAttrs(attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.BaseSegmentedBar);
        try{
            segmentedTextSize = getResources().getDimensionPixelSize(R.dimen.default_segmented_text_size);
            segmentedCheckedColor = ta.getColor(R.styleable.BaseSegmentedBar_segmentedSelectedColor,segmentedCheckedColor);
            segmentedTextSize = ta.getDimensionPixelSize(R.styleable.BaseSegmentedBar_segmentedTextSize,segmentedTextSize);
            segmentedCornerRadius = ta.getDimension(R.styleable.BaseSegmentedBar_segmentedCornerRadius, SegmentedBar.DEFAULT_CORNER_RADIUS);
            segmentedBorderWidth = ta.getDimensionPixelSize(R.styleable.BaseSegmentedBar_segmentedBorderWidth, SegmentedBar.DEFAULT_BORDER_WIDTH);
            segmentedItemWidth = ta.getDimensionPixelSize(R.styleable.BaseSegmentedBar_segmentedItemWidth, SegmentedBar.DEFAULT_SEGMENT_ITEM_WIDTH);
            segmentedItemHeight = ta.getDimensionPixelSize(R.styleable.BaseSegmentedBar_segmentedItemHeight, SegmentedBar.DEFAULT_SEGMENT_ITEM_HEIGHT);
            segmentCheckedPostion = ta.getInt(R.styleable.BaseSegmentedBar_segmentDefaultCheckedPostion,segmentCheckedPostion);
            Log.d("SamLog",segmentCheckedPostion + " <<< segmentCheckedPostion");
        } finally {
            ta.recycle();
        }
    }

    @Override
    protected void findView() {
        super.findView();
        segmentedBar = new SegmentedBar(getContext());
        segmentedBar.setOrientation(SegmentedBar.HORIZONTAL);
        segmentedBar.setTintColor(segmentedCheckedColor);
        segmentedBar.setUncheckedTextColor(segmentedCheckedColor);
        int bgColor = Color.BLACK;
        if(getBackground() instanceof ColorDrawable) bgColor = ((ColorDrawable)getBackground()).getColor();
        segmentedBar.setUncheckedTintColor(bgColor);
        segmentedBar.setCheckedTextColor(bgColor);
        segmentedBar.setSegmentNameSize(segmentedTextSize);
        segmentedBar.setBorderWidth(segmentedBorderWidth);
        segmentedBar.setCornerRadius(segmentedCornerRadius);
        segmentedBar.setSegmentItemWidth(segmentedItemWidth);
        segmentedBar.setSegmentItemHeight(segmentedItemHeight);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        segmentedBar.setLayoutParams(lp);
    }

    @Override
    public View getLeftView() {
        return null;
    }

    @Override
    public SegmentedBar getCenterView() {
        return segmentedBar;
    }

    @Override
    public View getRightView() {
        return null;
    }

    public void setSegmentInfo(String[] segmentNames,int defaultCheckedPos,SegmentedBar.OnSegmentCheckedChangeListener onSegmentCheckedChangeListener){
        segmentedBar.setSegmentInfo(segmentNames,defaultCheckedPos,onSegmentCheckedChangeListener);
    }

    //======================================================================


    public int getSegmentedCheckedColor() {
        return segmentedCheckedColor;
    }

    public void setSegmentedCheckedColor(int segmentedCheckedColor) {
        this.segmentedCheckedColor = segmentedCheckedColor;
        segmentedBar.setTintColor(segmentedCheckedColor);
    }

    public int getSegmentedTextSize() {
        return segmentedTextSize;
    }

    public void setSegmentedTextSize(int segmentedTextSize) {
        this.segmentedTextSize = segmentedTextSize;
        segmentedBar.setSegmentNameSize(segmentedTextSize);
    }

    public float getSegmentedCornerRadius() {
        return segmentedCornerRadius;
    }

    public void setSegmentedCornerRadius(float segmentedCornerRadius) {
        this.segmentedCornerRadius = segmentedCornerRadius;
        segmentedBar.setCornerRadius(segmentedCornerRadius);
    }

    public int getSegmentedBorderWidth() {
        return segmentedBorderWidth;
    }

    public void setSegmentedBorderWidth(int segmentedBorderWidth) {
        this.segmentedBorderWidth = segmentedBorderWidth;
        segmentedBar.setBorderWidth(segmentedBorderWidth);
    }

    public int getSegmentedItemWidth() {
        return segmentedItemWidth;
    }

    public void setSegmentedItemWidth(int segmentedItemWidth) {
        this.segmentedItemWidth = segmentedItemWidth;
        segmentedBar.setSegmentItemWidth(segmentedItemWidth);
    }

    public int getSegmentedItemHeight() {
        return segmentedItemHeight;
    }

    public void setSegmentedItemHeight(int segmentedItemHeight) {
        this.segmentedItemHeight = segmentedItemHeight;
        segmentedBar.setSegmentItemHeight(segmentedItemHeight);
    }

    public int getSegmentCheckedPostion() {
        return segmentCheckedPostion;
    }

    public void setSegmentDefaultCheckedPostion(int segmentDefaultCheckedPostion) {
        this.segmentCheckedPostion = segmentDefaultCheckedPostion;
        segmentedBar.setDefaultCheckedItem(segmentDefaultCheckedPostion);
    }
}
